package com.yanqu.road.dao.impl.activity.daomu;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.daomu.data.DaoMuActivityUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class DaoMuUserDataDaoImpl extends TempDataObjectDao<DaoMuActivityUserData> {


    public static String getDaoMuUserDataSql = "select * from `t_u_activity_daomu_user_data` where `activityId`= ?";
    public Map<Long, DaoMuActivityUserData> getDaoMuActivityUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, DaoMuActivityUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDaoMuUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DaoMuActivityUserData userData = parse(rs);
                map.put(userData.getUserId(), userData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getDaoMuUserDataInfoSql = "select * from `t_u_activity_daomu_user_data` where `activityId`= ? and `userId` = ?";
    public DaoMuActivityUserData getDaoMuActivityUserDataInfo(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        DaoMuActivityUserData userData = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDaoMuUserDataInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                userData = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return userData;
    }

    public static String insertDaoMuUserDataSql = "INSERT INTO `t_u_activity_daomu_user_data`(`activityId`, `userId`, `passesId`, `passPrePassesTime`, `endlessWave`, `roleId`, `energy`, `lastRestoreTime`, `prop`, `unlockRoles`, `serverId`, `progressBoxId`, `boxProgress`, `passAllFlag`, `createTime`, `collectionScore`, `level`, `historyMaxPassesId`, `killNumJson`, `drawBoxNum`, `roleJadeBfMap`, `firstRewardRecord`, `selectSkillId`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
    /**
     * 插入
     * @param data 数据对象
     * @return
     */
    @Override
    protected boolean addData(DaoMuActivityUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassesId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPassPrePassesTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndlessWave()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoleId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRestoreTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getProp()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnlockRoles()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getProgressBoxId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBoxProgress()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassAllFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCollectionScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHistoryMaxPassesId()));
        String killNumJson = "";
        if (data.getKillNumJson().size() > 0) {
            killNumJson = JSONObject.toJSONString(data.getKillNumJson());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, killNumJson));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawBoxNum()));
        String roleJadeBfMapJson = "";
        if (data.getRoleJadeBfMap().size() > 0) {
            roleJadeBfMapJson = JSONObject.toJSONString(data.getRoleJadeBfMap());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, roleJadeBfMapJson));
        //首通奖励记录
        String firstRewardRecord = "";
        if (data.getFirstRewardRecord().size() > 0) {
            firstRewardRecord = StringUtils.setToString(data.getFirstRewardRecord(), ";");
        }
        para.put(i++, new DbParameter(Types.VARCHAR, firstRewardRecord));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSelectSkillId()));
        if (getDb().executeNoneQuery(insertDaoMuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDaoMuUserDataSql = "UPDATE `t_u_activity_daomu_user_data` SET  `passesId` = ?, `passPrePassesTime` = ?, `endlessWave` = ?, `roleId` = ?, `energy` = ?, `lastRestoreTime` = ?, `prop` = ?, `unlockRoles` = ?, `progressBoxId` = ?, `boxProgress` = ?, `passAllFlag` = ?, collectionScore = ?, level = ?, historyMaxPassesId = ?, killNumJson = ?, `drawBoxNum` = ?, `roleJadeBfMap` = ?, `firstRewardRecord` = ?, `selectSkillId` = ?, updateTime = ? WHERE `activityId` = ? and `userId` = ?";
    /**
     * 更新
     * @param data
     * @return
     */
    @Override
    protected boolean updateData(DaoMuActivityUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassesId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPassPrePassesTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndlessWave()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoleId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRestoreTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getProp()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnlockRoles()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getProgressBoxId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBoxProgress()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassAllFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCollectionScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHistoryMaxPassesId()));
        String killNumJson = "";
        if (data.getKillNumJson().size() > 0) {
            killNumJson = JSONObject.toJSONString(data.getKillNumJson());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, killNumJson));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawBoxNum()));
        String roleJadeBfMapJson = "";
        if (data.getRoleJadeBfMap().size() > 0) {
            roleJadeBfMapJson = JSONObject.toJSONString(data.getRoleJadeBfMap());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, roleJadeBfMapJson));
        //首通奖励记录
        String firstRewardRecord = "";
        if (data.getFirstRewardRecord().size() > 0) {
            firstRewardRecord = StringUtils.setToString(data.getFirstRewardRecord(), ";");
        }
        para.put(i++, new DbParameter(Types.VARCHAR, firstRewardRecord));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSelectSkillId()));
        para.put(i++, new DbParameter(Types.BIGINT, System.currentTimeMillis()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateDaoMuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 解析数据
     * @param rs
     * @return
     * @throws SQLException
     */
    @Override
    protected DaoMuActivityUserData parseData(ResultSet rs) throws SQLException {
        DaoMuActivityUserData data = new DaoMuActivityUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setPassesId(rs.getInt("passesId"));
        data.setEndlessWave(rs.getInt("endlessWave"));
        data.setRoleId(rs.getInt("roleId"));
        data.setEnergy(rs.getInt("energy"));
        data.setLastRestoreTime(rs.getInt("lastRestoreTime"));
        data.setProp(rs.getString("prop"));
        data.setUnlockRoles(rs.getString("unlockRoles"));
        data.setServerId(rs.getLong("serverId"));
        data.setProgressBoxId(rs.getInt("progressBoxId"));
        data.setBoxProgress(rs.getInt("boxProgress"));
        data.setPassAllFlag(rs.getInt("passAllFlag"));
        data.setCreateTime(rs.getLong("createTime"));
        data.setCollectionScore(rs.getLong("collectionScore"));
        data.setLevel(rs.getInt("level"));
        data.setHistoryMaxPassesId(rs.getInt("historyMaxPassesId"));
        data.setUpdateTime(rs.getLong("updateTime"));
        //json解析
        String json = rs.getString("killNumJson");
        if (!StringUtils.isNullOrEmpty(json)) {
            Map<String, Integer> killNumJson = StringUtils.jsonToMap(json);
            data.setKillNumJson(killNumJson);
        }
        data.setDrawBoxNum(rs.getInt("drawBoxNum"));
        //json解析
        String roleJadeBfMapJson = rs.getString("roleJadeBfMap");
        if (!StringUtils.isNullOrEmpty(roleJadeBfMapJson)) {
            Map<String, Integer> roleJadeBfMap = StringUtils.jsonToMap(roleJadeBfMapJson);
            data.setRoleJadeBfMap(roleJadeBfMap);
        }
        //首通奖励记录
        String firstRewardRecord = rs.getString("firstRewardRecord");
        if (!StringUtils.isNullOrEmpty(firstRewardRecord)) {
            Set<Integer> set = StringUtils.stringToSet(firstRewardRecord, ";");
            data.setFirstRewardRecord(set);
        }
        //自选技能ID
        data.setSelectSkillId(rs.getInt("selectSkillId"));
        return data;
    }
}
